import React from 'react'
import store from '../store/index'
import {getAllGoodsList} from '../api/Home'
import type { GoodsListData } from '../types/home'


let goodsList = [] as GoodsListData[]
const getAllGoods = async ()=>{
    const res = await getAllGoodsList()
    goodsList = [...res.data]
}
getAllGoods()

const UseCartPrice = ()=>{
    const stortList=store.getState().cart
    const price=stortList.filter(item=>{ return item.state}).reduce((total,item)=>total+item.number*item.price,0)
    return price
}

const UseCartNumber=()=>{
    const stortList=store.getState().cart
    const number=stortList.reduce((total,item)=>total+item.number,0)
    return number
}

const UseisAllTrue = ()=>{
    const stortList=store.getState().cart
    return stortList.every(item=>item.state)
}

const UseSearch = (value:string)=>{
    return goodsList.filter(item=>item.proname.includes(value)||item.category === value)
}

export {UseCartPrice,UseCartNumber,UseisAllTrue,UseSearch}
